package com.ccrfid.rmc6.repository.personnel;

import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import org.hibernate.annotations.SQLUpdate;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Set;

public interface PersonnelRepository extends Repository<PersonnelEntity, Long> {
    //
    PersonnelEntity getByCardNumber(Long cardNumber);
    //
    PersonnelEntity getById(Long id);
    //
    PersonnelEntity getByPersonnelSn(String personnelSn);
    //
    List<PersonnelEntity> getAllByCardNumberNotNull();
    //
    List<PersonnelEntity> getAllByEnabled(Integer enabled);
    //
    List<PersonnelEntity> getAllByEnabledAndPersonnelTypeIn(Integer enabled, List<Integer> typeList);
    //
    List<PersonnelEntity> getAllByEnabledAndPersonnelType(Integer enabled, Integer personnelType);
    //
    List<PersonnelEntity> getAllByEnabledAndPersonnelTypeAndIdNotIn(Integer enabled, Integer personnelType, List<Long> idList);
    //
    Long countAllByIdInAndPersonnelTypeIn(Set<Long> idList, List<Integer> typeList);
    //
    Long countAllByIdInAndPersonnelTypeInAndEnabled(Set<Long> idList, List<Integer> typeList, Integer enabled);
    Long countAllByPersonnelTypeInAndEnabled(List<Integer> typeList, Integer enabled);
    //
    PersonnelEntity save(PersonnelEntity entity);
    //
    Boolean existsById(Long id);
    Boolean existsByCardNumber(Long cardNumber);
    Boolean existsByCardNumberAndIdNot(Long cardNumber, Long personnelId);
    Boolean existsByPersonnelSn(String personnelSn);
    Boolean existsByPersonnelSnAndIdNot(String personnelSn, Long personnelId);
    Boolean existsByIdNumber(String idNumber);
    Boolean existsByIdNumberAndIdNot(String idNumber, Long personnelId);
    Boolean existsByIdNumberAndIdNotAndEnabled(String idNumber, Long personnelId, Integer enabled);
    Boolean existsByIdNumberAndEnabled(String idNumber, Integer enabled);
    //
    void deleteById(Long id);

    @Transactional
    @Modifying
    @Query(value="UPDATE personnel  SET personnel_category = REPLACE(personnel_category,?1,'')  where  personnel_category LIKE CONCAT('%',?1,'%')",nativeQuery = true)
    void updatePersonnelCateGory(String personnelType);

    Integer countAllByPersonnelTypeInAndEnabledAndCardNumberIsNull(List<Integer> typeList, Integer enabled);

}
